<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->model('adminsessions');
	}

	public function index()
	{
		if(!$this->adminsessions->isLogedIn()){
			redirect(base_url()."admin/home/login");
		}
		else{
			$this->load->view('admin/home/dashboard');
		}
		
	}

	public function login()
	{
		if(!$this->adminsessions->isLogedIn()){
			$data["error"] = "";
			if(isset($_POST['adminemail']) && isset($_POST['adminpass']))	{
				//Process admin login
				$rs = $this->process_data->matchAndGetAdminDetail($this->input->post('adminemail'), $this->input->post('adminpass'));
				if(count($rs)>0){
					$this->adminsessions->setSessionData($this->input->post('adminemail'), $rs[0]['id'], 'admin');
					redirect(base_url()."admin/home");
				}else{
					$data["error"] = "Username or Password Error";
				}
			}
			
			$this->load->view('admin/home/login', $data);
		}
		else{
			redirect(base_url()."admin/home");
		}
	}
	
	public function logout()
	{
		$this->adminsessions->logOut();
		redirect(base_url()."admin/home");
	}
}